package com.wzt.lianfirecontrol.adapter.xiaokongshi;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.function.xiaokongshi.XksRecordProblemModel;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.Utils;

public class XksProblemAdapter extends BaseRecyclerAdapter<XksRecordProblemModel> {


    private Context context;

    private boolean needSetTopMargin = false;

    private boolean isDetail = false;

    public boolean isNeedSetTopMargin() {
        return needSetTopMargin;
    }


    public boolean isDetail() {
        return isDetail;
    }

    public void setDetail(boolean detail) {
        isDetail = detail;
    }

    public void setNeedSetTopMargin(boolean needSetTopMargin) {
        this.needSetTopMargin = needSetTopMargin;
    }

    public XksProblemAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_info_list, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, XksRecordProblemModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout ll_item_content;
        private TextView tv_title;
        private ImageView iv_show_goto;
        private TextView tv_des;
        private LinearLayout ll_time;
        private ImageView iv_time;
        private TextView tv_time;


        public MyViewHolder(View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.tv_title = itemView.findViewById(R.id.tv_title);
        myViewHolder.iv_show_goto = itemView.findViewById(R.id.iv_show_goto);
        myViewHolder.tv_des = itemView.findViewById(R.id.tv_des);
        myViewHolder.ll_time = itemView.findViewById(R.id.ll_time);
        myViewHolder.iv_time = itemView.findViewById(R.id.iv_time);
        myViewHolder.tv_time = itemView.findViewById(R.id.tv_time);
    }

    private void setItemData(MyViewHolder viewHolder, XksRecordProblemModel data, int position) {
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();
        if (isDetail){
            viewHolder.ll_item_content.setBackgroundColor(context.getResources().getColor(R.color.white));
            if (0 == position && needSetTopMargin){
                layoutParams.setMargins(Utils.dip2px(context, 15), Utils.dip2px(context, 0.5f),
                        Utils.dip2px(context, 15), Utils.dip2px(context, 0.5f));
            }else {
                layoutParams.setMargins(Utils.dip2px(context, 15), Utils.dip2px(context, 0),
                        Utils.dip2px(context, 15), Utils.dip2px(context, 0.5f));
            }
        }else {
            viewHolder.ll_item_content.setBackground(context.getResources().getDrawable(R.drawable.shape_white_bg_frame));
            if (0 == position && needSetTopMargin){
                layoutParams.setMargins(Utils.dip2px(context, 0), Utils.dip2px(context, 15f),
                        Utils.dip2px(context, 0), Utils.dip2px(context, 15f));
            }else {
                layoutParams.setMargins(Utils.dip2px(context, 0), Utils.dip2px(context, 0),
                        Utils.dip2px(context, 0), Utils.dip2px(context, 15f));
            }
        }

        if (StringUtils.isEmpty(data.getName())){
            viewHolder.tv_title.setVisibility(View.GONE);
        }else {
            viewHolder.tv_title.setVisibility(View.VISIBLE);
            viewHolder.tv_title.setText(data.getName());
        }

        if (StringUtils.isEmpty(data.getRemark())){
            viewHolder.tv_des.setVisibility(View.GONE);
        }else {
            viewHolder.tv_des.setVisibility(View.VISIBLE);
            viewHolder.tv_des.setText(data.getRemark());
        }

        if (StringUtils.isEmpty(data.getTimeStr())){
            viewHolder.ll_time.setVisibility(View.GONE);
        }else {
            viewHolder.iv_time.setVisibility(View.VISIBLE);
            viewHolder.ll_time.setVisibility(View.VISIBLE);
            viewHolder.tv_time.setText(data.getTimeStr());
        }
        viewHolder.iv_show_goto.setVisibility(View.VISIBLE);
    }
}
